package com.kolon.business.mngr.client.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.client.dao.ClientDao;
import com.kolon.framework.constant.Constant;
import com.kolon.framework.file.DefaultFileRenamePolicy;
import com.kolon.framework.file.FileUtil;
import com.kolon.framework.file.UploadedFile;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mngr.client.controller.ClientModifyController")
public class ClientModifyController implements SimpleController {

	@Resource(name="com.kolon.business.mngr.client.dao.ClientDaoImpl")
	private ClientDao clientDao;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/client/clientModify.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		Map<String, String> map = new HashMap<String, String>();
		List<UploadedFile> fileList = new ArrayList<UploadedFile>();
		// 파라미터
		String frfVer = multiRequest.getParameter("frfVer");
		String frfTp = multiRequest.getParameter("frfTp");
		map.put("appCntDesc", ComUtil.nullChk(multiRequest.getParameter("appCntDesc")));
		map.put("frfVer", frfVer);
		map.put("frfTp", frfTp);
		// 회원 세션 정보
		HttpSession httpSession = multiRequest.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_MNGR_INFO);
		map.put("mdfyId", (String)userMap.get("MNGR_ID"));
		// 정보 조회 값 체크
		Map cntMap = clientDao.getMap(map);
		if (cntMap == null)
			return new ModelAndView("redirect:/business/mngr/client/clientModifyForm.do?frfTp="+map.get("frfTp"));
		// 파일 저장 경로
		String appFilePath = MsgUtil.getMessage("app.file.path");
		appFilePath = appFilePath + frfTp.toLowerCase() + File.separator + "kolonapp" + File.separator;
		// 버전 중복 체크 함
		if (!frfVer.equals(cntMap.get("FRF_VER"))) {
			if (clientDao.getAppHstyCnt(map) > 0)
				return new ModelAndView("redirect:/business/mngr/client/clientModifyForm.do?frfTp="+map.get("frfTp"));
		}
		// STEP START
		try {
			// STEP 01. 앱 파일 업로드
			for (int i=1; i<=3; i++) {
				MultipartFile mFile = multiRequest.getFile("appFile"+i);
				// 업로드 시 기존 파일 삭제
				if (!"".equals(mFile.getOriginalFilename()))
					FileUtil.delFile(appFilePath + cntMap.get("FILE_S_NM_"+i));
				UploadedFile uploadedFile = FileUtil.fileUpload(mFile, 
						appFilePath, new DefaultFileRenamePolicy());
				map.put("fileRNm"+i, uploadedFile.getFileRealName());
				map.put("fileSNm"+i, uploadedFile.getFileName());
				map.put("fileTp"+i, uploadedFile.getExtension());
				map.put("fileSize"+i, String.valueOf(uploadedFile.getSize()));
				fileList.add(uploadedFile);
			}
			// STEP 02. 정보 수정
			clientDao.update(map);
		} catch (Exception e) {
			// 예외 발생 시 업로드 된 파일을 삭제
			for (int i=0, size=fileList.size(); i<size; i++)
				FileUtil.delFile(appFilePath + fileList.get(i).getFileName());
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/business/mngr/client/clientModifyForm.do");
		modelAndView.addObject("frfTp", frfTp);
		return modelAndView;
	}
}
